/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef LOG_H
#define LOG_H

#include <QString>
#include <QFile>
#include <QTextStream>

class LogParam
{

public:
    QString category;   // appender
    QString source;     // module
    QString method;     //
    QString type;       // enter function, exit, etc

};


class TLogger
{

public:
    enum ELogLevel { TRACE, DEBUG, INFO, WARNING, ERROR, FATAL };


    bool isEnabled();
    void enable(const bool how);
    ELogLevel getLevel();
    void setLevel(const ELogLevel newLevel);
    QString getFile();
    void setFile(const QString& newFile);
    bool isLoggable(const ELogLevel level);
    QString levelToString(const ELogLevel level);


    void trace(const QString &message);
    void debug(const QString &message);
    void info(const QString &message);
    void warning(const QString &message);
    void error(const QString &message);
    void fatal(const QString &message);

    void trace(const LogParam *source, const QString &message);
    void debug(const LogParam *source, const QString &message);
    void info(const LogParam *source, const QString &message);
    void warning(const LogParam *source, const QString &message);
    void error(const LogParam *source, const QString &message);
    void fatal(const LogParam *source, const QString &message);

    void log(const ELogLevel level, const LogParam *source, const QString &message);

    TLogger(const bool isEnabled, const QString &tag, const QString &fileName);
    ~TLogger();

private:
    QString     _tag;
    ELogLevel   _level;
    bool        _enabled;
    QString     _fileName;
    QFile        _file;
    QTextStream  _stream;
    bool        _init;
    bool        _isWriteImmediate;


    void open();
    void close();
    void start();
    void notifyError();
};

#endif // LOG_H
